package com.itheima.controller;



import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController

public class StudentController {
   @Autowired
    private StudentService studentService;
   @GetMapping()
    public Result getStudents(StudentQueryParam studentQueryParam) {
       log.info("根据条件查询学生:{}", studentQueryParam);
       PageResult<Student> studentList = studentService.page(studentQueryParam);
       return Result.success(studentList);
   }
   @PostMapping
    public Result addStudent(@RequestBody Student student) {
       log.info("添加学生:{}", student);
       studentService.add(student);
       return Result.success();
   }
   @DeleteMapping("/{ids}")
    public Result deleteStudent(@RequestParam List<Integer> ids) {
       log.info("根据id删除学生", ids);
       studentService.deleteById(ids);
       return Result.success();
   }
   @GetMapping("/{id}")
    public Result getStudentById(@PathVariable Integer id) {
       log.info("根据id查询学生 {}", id);
       Student student = studentService.getStuById(id);
       return Result.success(student);
   }
   @PutMapping
    public Result updateStudent(@RequestBody Student student) {
       log.info("修改学员信息:{}", student);
       studentService.update(student);
       return Result.success();
   }
   @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Short id, @PathVariable short score) {
       log.info("违纪学生处理 {} {}", id,score);
       studentService.updateVioIdandScore(id,score);
       return Result.success();
   }
}
